package com.example.blog;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class BookInfoRetriever {

    private static final String ISBNDB_API_URL = "http://isbndb.com/api/v2/json/YOUR_API_KEY/book/";
    private static final String API_KEY = "YOUR_API_KEY"; // 替换为您的API密钥

    public static void main(String[] args) {
        // 示例ISBN
        String isbn = "9780134685991";
        getBookInfo(isbn);
    }

    public static void getBookInfo(String isbn) {
        HttpClient client = HttpClients.createDefault();
        HttpGet request = new HttpGet(ISBNDB_API_URL + isbn);

        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                String result = EntityUtils.toString(entity);
                JSONObject jsonObject = new JSONObject(result);
                JSONObject dataObject = jsonObject.getJSONArray("data").getJSONObject(0);

                String title = dataObject.getString("title");
                String authors = dataObject.getJSONArray("author_data").toString(); // 作者可能是一个列表
                String price = dataObject.optString("price", "Not available");

                System.out.println("Title: " + title);
                System.out.println("Authors: " + authors);
                System.out.println("Price: " + price);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
